// 通过postMessage调用content-script
function invokeContentScript(code) {
    window.postMessage({ cmd: 'invoke', code: code }, '*');
}
// 发送普通消息到content-script
function sendMessageToContentScriptByPostMessage(data) {
    window.postMessage({ cmd: 'message', data: data }, '*');
}

function openDiv() {
    // alert('打开DIV……' + document.body.clientWidth);
    var divpanel = document.createElement('div');
    var boxCollection = document.getElementsByTagName("a");
    // console.log(boxCollection[0]['text'])
    var str = "";
    for (var j = 0, len = boxCollection.length; j < len; j++) {
        // console.log(boxCollection[j]['text']);
        str = str + "<h4><a>" + boxCollection[j]['text'] + "</a></h4>"
    }
    divpanel.className = 'chrome-plugin-demo-panel';
    divpanel.innerHTML = `<div style="width:1200px;height:200px;margin-right:10px;position:right;border: solid 1px;overflow-y: scroll;overflow-x: scroll">` + str + `</div>`;
    document.body.appendChild(divpanel);
}

// 通过DOM事件发送消息给content-script
(function () {
    var customEvent = document.createEvent('Event');
    customEvent.initEvent('myCustomEvent', true, true);
    // 通过事件发送消息给content-script
    function sendMessageToContentScriptByEvent(data) {
        data = data || '你好，我是injected-script!';
        var hiddenDiv = document.getElementById('myCustomEventDiv');
        hiddenDiv.innerText = data;
        hiddenDiv.dispatchEvent(customEvent);
    }
    window.sendMessageToContentScriptByEvent = sendMessageToContentScriptByEvent;
})();
